<?php

//------------------------
// 会员级别控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\UserRank as UserRankModel;
use app\common\model\UserRankPrice as UserRankPriceModel;
use think\Config;
use think\Db;

class UserRank extends AdminBase
{
    protected $UserModel;
    protected $UserRankPriceModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->UserRankModel               = new UserRankModel();
        $this->UserRankPriceModel          = new UserRankPriceModel();
    }


    // 列表
    public function index(){

        $info = $this->UserRankModel->paginate(10,false);

        $this->assign('info',$info);

        return $this->fetch('index');
    }

    // 添加页面
    function add(){
        return $this->fetch('add');
    }

    /**
     * @desc 添加操作
     */
    public function addProc() {
        $data            = $this->request->param();

        $validate_result = $this->validate($data, 'UserRank');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $res = $this->UserRankModel->save($data);

        if ($res === false) {
            $this->error($this->UserRankModel->getError());
        } else {
            $this->success('添加成功');
        }
    }


    // 编辑页面
    function edit(){
        $id = $this->request->param('id',0,'intval');

        $data = $this->UserRankModel->get($id);

        $this->assign('data',$data);
        return $this->fetch('edit');
    }

    // 编辑操作
    function editProc(){
        $data  = $this->request->param('');

        //根据传入的scene生成不同的场景
        $validate_result = $this->validate($data, 'UserRank');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        // allowField表示过滤非数据表中字段的数据
        $res = $this->UserRankModel->allowField(true)->save($data,$data['id']);

        if ($res === false) {
            $this->error($this->UserRankModel->getError());
        } else {
            $this->success('保存成功');
        }
    }



    // 删除操作
    public function delete(){
        $id = $this->request->param('id',0,'intval');

        $where['id'] = array('eq',$id);

        $res = $this->UserRankModel->where($where)->delete();

        if ($res == false) {
            $this->error($this->UserRankModel->getError());
        } else{
            $this->success('删除成功');
        }
    }

    // 修改会员级别价格
    public function editPrice(){
        $data = $this->request->param('');

        // 先将所有当前商品的会员价格全部删掉
        $this->UserRankPriceModel->where('goods_id',$data['id'])->delete();

        $user_rank_price = isset($data['user_rank_price'])? $data['user_rank_price'] : '';
        $i       = 0;
        $newData = array();

        if($user_rank_price){
            foreach ($user_rank_price as $k => $v){

                if($v == ''){
                    continue ;
                }

                $newData[$i] = array(
                    'goods_id'      => $data['id'],
                    'user_rank_id'  => $k,
                    'price'         => $v,
                );

                $validate_result = $this->validate($newData[$i], 'UserRankPrice');

                if ($validate_result !== true) {
                        $this->error($validate_result);
                }

                $i = ++$i;
            }
        }

        $addRes = $this->UserRankPriceModel->saveAll($newData);

        if ( $addRes === false ) {
            $this->error($this->UserRankPriceModel->getError());
        } else {
            $this->success('保存成功');
        }
    }





}